package ModelLayer;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Write a description of class Order here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Order
{
    private ArrayList<PartOrder> partOrders;
    private int orderId;
    private String date;
    private double discount;
    private Person employee;
    private Person customer;

    /**
     * Constructor for objects of class order.
     * Contains arraylist of partorders.
     * @param discount, the discount on the order.
     * @param employee, the employee that handles this order.
     * @param customer, the customer associated to this order.
     * 
     */
    public Order(double discount, Person employee, Person customer)
    {
        partOrders = new ArrayList<PartOrder>(); 
        this.orderId = orderId;
        this.date = getDateTime();
        this.discount = discount;
        this.employee = employee;
        this.customer = customer;
    }

    /**
     * Returns orderId.
     * @return orderId, the id that identifies this order.
     */
    public int getOrderId()
    {
        return orderId;
    }

    /**
     * Returns date.
     * @return date, the date of this order.
     */
    public String getDate()
    {
        return date;
    }

    /**
     * Returns discount.
     * @return discount, the discount on this order.
     */
    public double getDiscount()
    {
        return discount;
    }

    /**
     * Sets new orderId if needed.
     * @param orderId, the orderId to be set.
     */
    public void setOrderId(int orderId)
    {
        this.orderId = orderId;
    }

    /**
     * Sets new date if needed.
     * @param date, the date to be set.
     */
    public void setDate(String date)
    {
        this.date = date;
    }

    /**
     * Sets new discount if needed.
     * @param discount, the discount to be set.
     */
    public void setDiscount(double discount)
    {
        this.discount = discount;
    }

    /**
     * Adds partOrder to this order.
     * @param partOrder, the partorder to be added.
     */
    public void addToOrder(PartOrder partOrder)
    {
        partOrders.add(partOrder);
    }

    /**
     * Returns totalPrice of all partorders.
     * @return totalPrice, the totalprice returned.
     */
    public double getTotalPrice()
    {
        double totalPrice = 0;
        for (PartOrder partOrder : partOrders)
        {
            totalPrice = (totalPrice + partOrder.getPrice());
        }
        double discountInMoney = totalPrice / 100 * discount;
        totalPrice = totalPrice - discountInMoney;
        return totalPrice;
    }

    /**
     * Calculates discount and returns the amount in actual money.
     * @return discountInMoney, the discount returned in money.
     */    
    public double getDiscountInMoney()
    {
        double totalPrice = 0;
        for (PartOrder partOrder : partOrders)
        {
            totalPrice = totalPrice + partOrder.getPrice();
        }
        double discountInMoney = totalPrice / 100 * getDiscount();
        return discountInMoney;
    }

    /**
     * Downsizes inventory for items on all partorders.
     */
    public void downsizeInventory()
    {
        for(PartOrder partOrder : partOrders)
        {
            partOrder.downsizeInventory();
        }
    }

    /**
     * Returns arraylist containing all partorders on this order.
     * @return partOrders, arraylist returned
     */
    public ArrayList<PartOrder> getPartOrders()
    {
        return partOrders;
    }

    /**
     * Returns current date and time.
     * @return dateFormat.format(date), current date and time returned
     */
    private String getDateTime() 
    {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
}

